//
//  WBEmoticonBottomView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/20.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBEmoticonBottomView.h"


@interface WBEmoticonBottomView ()

@property (nonatomic , strong) UIButton *currentBtn;

@end

@implementation WBEmoticonBottomView


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        
        [self setupUI];
    
    }
    
    return self;

}

- (void)setupUI
{
    
    self.backgroundColor = [UIColor randomColor];
    
    //设置填充方向
    self.axis = UILayoutConstraintAxisHorizontal;
    
    //设置填充方式
    self.distribution = UIStackViewDistributionFillEqually;
    [self addChildButtonWithImageName:@"left" andTitle:@"默认" andTag:Default];
    [self addChildButtonWithImageName:@"mid" andTitle:@"emoji" andTag:Emoji];
    [self addChildButtonWithImageName:@"right" andTitle:@"浪小花" andTag:Lxh];
    
}

- (void)addChildButtonWithImageName:(NSString *)imageName andTitle:(NSString *)title andTag:(WBEmoticonBottomViewType)type
{
    UIButton *btn = [[UIButton alloc] init];
    
    btn.showsTouchWhenHighlighted = YES;
    

    btn.tag = (int)type + typeNumber;
    //添加按钮点击事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置图片compose_emotion_table_left_selected compose_emotion_table_left_normal
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"compose_emotion_table_%@_normal",imageName]] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"compose_emotion_table_%@_selected",imageName]] forState:UIControlStateSelected];
    
    //设置文字
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    //默认第一个选中
    if(type == Default)
    {
        btn.selected = YES;
        self.currentBtn = btn;
    
    }
    
    [self addArrangedSubview:btn];

}

//按钮点击事件
- (void)btnClick:(UIButton *) btn
{
    //如果两个按钮一样 直接返回
    if(self.currentBtn == btn)
    {
        return ;
    }

    //取消以前按钮的高亮状态
    self.currentBtn.selected = NO;
    
    //设置当前按钮的高亮状态
    btn.selected = YES;
    
    //赋值
    self.currentBtn = btn;
    
    //执行block
    self.block((WBEmoticonBottomViewType)(btn.tag - typeNumber));

}

- (void)setSelectedBtnWithTag:(int)tag
{
    
    UIButton *btn = (UIButton *)[self viewWithTag:tag];
    //如果两个按钮一样 直接返回
    if(self.currentBtn == btn)
    {
        return ;
    }
    
    //取消以前按钮的高亮状态
    self.currentBtn.selected = NO;
    
    //设置当前按钮的高亮状态
    btn.selected = YES;
    
    //赋值
    self.currentBtn = btn;

}




@end
